#include <rclcpp/rclcpp.hpp>
#include <my_custom_msgs/msg/time_info.hpp>

class TimeSubscriberNode : public rclcpp::Node
{
public:
    TimeSubscriberNode() : Node("time_subscriber")
    {
        // 创建订阅器
        subscription_ = this->create_subscription<my_custom_msgs::msg::TimeInfo>(
            "time_info", 10, std::bind(&TimeSubscriberNode::time_info_callback, this, std::placeholders::_1));
    }

private:
    void time_info_callback(const my_custom_msgs::msg::TimeInfo::SharedPtr msg)
    {
        RCLCPP_INFO(this->get_logger(), "收到的当前时间: %s, 经过时长: %s", msg->current_time.c_str(), msg->elapsed_time.c_str());
    }

    rclcpp::Subscription<my_custom_msgs::msg::TimeInfo>::SharedPtr subscription_;
};

int main(int argc, char **argv)
{
    // 初始化ROS 2
    rclcpp::init(argc, argv);
    
    // 创建并运行节点
    rclcpp::spin(std::make_shared<TimeSubscriberNode>());
    
    // 清理资源
    rclcpp::shutdown();
    return 0;
}

